<?php
require_once('./class/News.php');
require_once('./class/CommentNews.php');
require_once('./class/Individu.php');
switch (@$_GET['action']){
    default:
        echo "NEWS";
    break;

    case "addForm":
        echo News::getAddFormAssociation();
        break;

    case "addNews":
        echo News::getAddForm($_POST['id_association']);
    break;
    
    case "doAdd":
        $n = new News();
        $n->setIdIndividu($_POST['id_individu']);
        $n->setIdAssociation($_POST['id_association']);
        $n->setTexte($_POST['texte']);
        $n->setTitre($_POST['titre']);
        $n->addOrModify();
        echo _NEWS_.' '._ADDED_;
        break;

    case "list":
        $news_array = News::getAllNews();
        $html = '<table class="tablesorter">
        			<thead>
        			<th>'._ID_NEWS_.'</th>
        			<th>'._INDIVIDU_.'</th>
        			<th>'._ASSOCIATION_.'</th>
        			<th>'._DATE_NEWS_.'</th>
        			<th>'._TITRE_.'</th>
        			<th>'._TEXTE_.'</th>
        			<th>'._COMMENTAIRE_.'</th>
        			<th>'._EDIT_.'</th>
        			<th>'._DELETE_.'</th>
        		</thead><tbody>';
        $i = 0;
        foreach($news_array as $n){
            $odd = ($i % 2 == 0) ? 'class="odd"' : "";
            $i++;
            $html .= '<tr '.$odd.'>
        					<td>'.$n->getIdNews().'</td>
        					<td>'.Individu::getIndividu($n->getIdIndividu())->getNom().' '.Individu::getIndividu($n->getIdIndividu())->getPrenom().'</td>
        					<td>'.Association::getAssociation($n->getIdAssociation())->getNom().'</td>
        					<td>'.$n->getDateNews().'</td>
        					<td>'.$n->getTitre().'</td>
        					<td>'.$n->getTexte().'</td>
        					<td><a href="?module=commentNews&action=list&id='.$n->getIdNews().'">'._VOIR_.' ('.CommentNews::getNbComment($n->getIdNews()).')</a></td>
        					<td><a href="?module=news&action=modifyForm&id='.$n->getIdNews().'">'._EDIT_.'</a></td>
        					<td><a href="?module=news&action=delete&id='.$n->getIdNews().'">'._DELETE_.'</a></td>
	        			</tr>';
        }
        $html .= '</tbody></table>';
        echo $html;
        break;
    case "modifyForm":
        $n = News::getNews($_GET['id']);
        echo $n->getModifyForm();
        break;

    case "doModify":
        $n = new News();
        $n->setIdNews($_POST['id_news']);
        $n->setIdAssociation($_POST['id_individu']);
        $n->setIdIndividu($_POST['id_individu']);
        $n->setDateNews($_POST['date']);
        $n->setTexte($_POST['texte']);
        $n->setTitre($_POST['titre']);
        $n->addOrModify();
        echo _NEWS_.' '. _MODIFIED_;
        break;

    case "delete":
        $n = News::getNews($_GET['id'], 'id_news');
        $n->delete();
        echo _NEWS_.' '. _DELETED_;
        break;
}
?>
